package ebcs.scan.controller.needCtr;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import ebcs.database.bean.BsGroupInfor;
import ebcs.database.bean.BsNeedInfor;
import ebcs.database.bean.BsNeedTime;
import ebcs.database.bean.BsUserInfor;
import ebcs.database.bean.BsUserNeedRel;
import ebcs.database.bean.RpProjectMoneyBean;
import ebcs.database.service.BsUserInforServiceImp;
import ebcs.database.service.BsUserNeedServiceImp;
import ebcs.database.service.impl.BsUserInforServiceImpl;
import ebcs.database.service.impl.BsUserNeedServiceImpl;
import ebcs.scan.common.utils.StringUtil;
import ebcs.scan.controller.batch.BatchManagerCtrl;
import jplat.core.trans.JAppBaseService;
import jplat.core.trans.JAppContext;
import jplat.core.trans.JAppContextFactory;
import jplat.error.exception.JSystemException;
import jplat.error.exception.JTransException;
import jplat.tools.string.JRandomUtil;
 

/***
 * 需求管理
 * @author chenzhuo
 * @comment
 */
@Controller
public class NeedManagerCtrl extends JAppBaseService
{
	private Logger logger = Logger.getLogger(NeedManagerCtrl.class);
	protected JAppContextFactory serviceFactory = new JAppContextFactory();

	@Autowired
	public BatchManagerCtrl batch;
	//数据库库服务.
	@Autowired 
	public BsUserInforServiceImp userInforService ; 
	@Autowired
	private BsUserNeedServiceImp userNeedService;
 
	
	public static String CHARSET_ENCODING = "UTF-8";
	
	/**
	 * 新增需求功能 
	 */
	@RequestMapping("/page/need/doMakeNeed.do")
	public String doMakeNeed( HttpServletRequest request,HttpServletResponse response ) throws JTransException, JSystemException{
		JAppContext context = serviceFactory.buildAppContextWithUser(request,response);		//buildAppContextWithUser 自带客户信息.
		BsUserInfor user = context.getUserInfo();
		request.setAttribute("userBean", user);
		return "frame/html/need/makeNeedInfor";
	}
	   
	/**
	 * 修改需求功能 
	 */
	@RequestMapping("/page/need/{needId}/doUpdateNeedInfor.do")
	public String doUpdateNeedInfor( HttpServletRequest request,HttpServletResponse response ,@PathVariable String needId) throws JTransException, JSystemException{
		JAppContext context = serviceFactory.buildAppContextNoUser(request,response);		//buildAppContextWithUser 自带客户信息.
  		List<BsNeedInfor> needList = this.userInforService.selectByNeedId(needId);
 		request.setAttribute("needBean", needList.get(0));
		return "frame/html/need/updateNeedInfor"; 
	}
	
	/**
	 * 删除需求功能 
	 */
	@RequestMapping("/page/need/dodeleteInfor.do")
	public void dodeleteInfor( HttpServletRequest request,HttpServletResponse response) throws JTransException, JSystemException{
		JAppContext context = serviceFactory.buildAppContextNoUser(request,response);		//buildAppContextWithUser 自带客户信息.
		BsUserNeedRel bsUserNeedRel = context.obtainReqModel(BsUserNeedRel.class);
		List<BsUserNeedRel> needList1 = userNeedService.selectByNeedId(bsUserNeedRel.getRouid());
		String tishi;
		if(needList1.size()==0){
			userNeedService.deleteInfor(bsUserNeedRel.getRouid());
			tishi="删除成功";
		}else{
			tishi="请先删除报功记录";
		}
		request.setAttribute("tishi", tishi);
		Map rspModel = new HashMap();
		rspModel.put("tishi", tishi);
        context.buildAppResponse(rspModel); 
	}
	/**
	 * 需求展示 
	 */
	@RequestMapping("/page/need/doShowNeedInfor.do")
	public String doShowNeedInfor( HttpServletRequest request,HttpServletResponse response ) throws JTransException, JSystemException{
 		JAppContext context = serviceFactory.buildAppContextWithUser(request,response);		//buildAppContextWithUser 自带客户信息.
 		String needName = request.getParameter("needName");
 		BsUserInfor user = context.getUserInfo();
 		BsNeedInfor req = new BsNeedInfor();
 		req.setProTeamNo(user.getProTeamNo());
 		if(StringUtil.isNotEmpty(needName)){
	 		req.setNeedName(needName);
	 		req.setNeedNo(needName);
 		}
		List<BsNeedInfor> needList = this.userNeedService.selectNeedInfor(req);
		request.setAttribute("needList", needList);
		return "frame/html/need/showNeedInfor";
	}
	
	/**
	 * 需求新增功能
	 * @param request
	 * @param response
	 * @throws JTransException
	 * @throws JSystemException
	 */
	@RequestMapping("/page/need/addNeedInfor.do")
	public void addNeedInfor( HttpServletRequest request,HttpServletResponse response ) throws JTransException, JSystemException{
 		JAppContext context = serviceFactory.buildAppContextNoUser(request,response);		//buildAppContextWithUser 自带客户信息.
 		BsNeedInfor req = context.obtainReqModel(BsNeedInfor.class);

 		//1、判断日期格式和工时是否合法。
 		if(!org.apache.commons.lang3.StringUtils.isNumeric(req.getNeedTime())){
 			throw new  JTransException("1001","你又乱填数字了哦，工时必须是数字！");
  		} 
 		//2、判断一个项目组一个需求数据唯一
   		List<BsNeedInfor> needList = this.userNeedService.selectNeedInforByNeedNoAndTeamNo(req);
  		if(needList.size()>0){
  			throw new  JTransException("1001","需求编号:"+req.getNeedNo()+"在该项目组中已经存在了！");
  		} 
 		
 		BsNeedInfor record = new BsNeedInfor();
 		record.setRouid(JRandomUtil.getUUID());
  		record.setNeedNo(req.getNeedNo());
 		record.setNeedTime(req.getNeedTime()); 
 		record.setProTeamNo(req.getProTeamNo());
 		record.setNeedName(req.getNeedName());
 		//00表示未审核
 		record.setState("00");
 		//发送审核通知给团队经理
 		//获取登录用户的名称
 		Object obj = request.getSession().getAttribute("loginUserName");
 		batch.sendAddNeedApplyToGroupUser(obj == null?"":obj.toString(), req.getProTeamNo());
 		userNeedService.addNeedInfor(record);
 		
 		Map rspModel = new HashMap();
		context.buildAppResponse(rspModel);
	}
	
	
	
	/**
	 * 修改需求功能 
	 */
	@RequestMapping("/page/need/updateNeedInfor.do")
	public void updateNeedInfor( HttpServletRequest request,HttpServletResponse response ) throws JTransException, JSystemException{
		//根据rouid 修改需求的名称、需求编号、需求评估工时
		JAppContext context = serviceFactory.buildAppContextNoUser(request,response);		//buildAppContextWithUser 自带客户信息.
		BsNeedInfor req = context.obtainReqModel(BsNeedInfor.class);
		this.userNeedService.updateNeedInforById(req);
		
		this.userNeedService.updateNeedNoRelByNeedId(req);
		Map rsModel = new HashMap();
		context.buildAppResponse(rsModel);
	}
	

	
	/**
	 * 查询登陆用户所属需求功能列表 
	 */
	@RequestMapping("/page/need/searchNeedInfor.do")
	public void searchNeedInfor( HttpServletRequest request,HttpServletResponse response ) throws JTransException, JSystemException{
		JAppContext context = serviceFactory.buildAppContextWithUser(request,response);		//buildAppContextWithUser 自带客户信息.
		BsUserInfor user = context.getUserInfo();
 		BsNeedInfor req = context.obtainReqModel(BsNeedInfor.class);
 		req.setProTeamNo(user.getProTeamNo());
		List<BsNeedInfor> needList = this.userNeedService.selectNeedInfor(req);
		Map rsModel = new HashMap();
		rsModel.put("needList", needList);
 		context.buildAppResponse(rsModel);
	}
	
	/**
	 * 查询所选项目组所属需求功能列表 
	 */
	@RequestMapping("/page/need/searchNeedInforDetail.do")
	public void searchNeedInforDetail( HttpServletRequest request,HttpServletResponse response ) throws JTransException, JSystemException{
		JAppContext context = serviceFactory.buildAppContextNoUser(request,response);
 		BsNeedInfor req = context.obtainReqModel(BsNeedInfor.class);
		List<BsNeedInfor> needList = this.userNeedService.selectNeedInforDetail(req);
		context.buildAppResponse(needList);
	}
	/**
	 * 删除需求信息
	 */
	@RequestMapping("/page/need/{needId}/doDeleteNeed.do")
	public void doDeleteNeed( HttpServletRequest request,HttpServletResponse response,@PathVariable String needId ) throws JTransException, JSystemException{
		//根据rouid 删除需求信息
 		JAppContext context = serviceFactory.buildAppContextNoUser(request,response);
 		
 		this.userNeedService.deleteUserNeedInfor(needId);
 		Map rsModel = new HashMap();
 		context.buildAppResponse(rsModel);
	}
	
	/**
	 * 查下需求时间轴信息
	 */
	@RequestMapping("/page/need/selectNeedTime.do")
	public void selectNeedTime( HttpServletRequest request,HttpServletResponse response ) throws JTransException, JSystemException{
  		JAppContext context = serviceFactory.buildAppContextWithUser(request,response);		//buildAppContextWithUser 自带客户信息.
  		List<BsNeedTime> needTimeList = this.userNeedService.selectNeedTimeList(context.getUserInfo());
  		Map rsModel = new HashMap();
  		rsModel.put("needTimeList", needTimeList);
 		context.buildAppResponse(rsModel);
	}
	
	@RequestMapping("/page/need/updateStateByRouid.do")
	@ResponseBody
	public String updateStateByRouid(HttpServletRequest request,HttpServletResponse response){
		String state = request.getParameter("state");
		String rouid = request.getParameter("rouid");
		if(state == null || "".equals(state) || rouid == null || "".equals(rouid)){
			return "{\"status\":\"faild\"}";
		}
		int row = this.userNeedService.updateStateByRouid(rouid, state);
		if(row == 0){
			return "{\"status\":\"faild\"}";
		}else{
			return "{\"status\":\"success\"}";
		}
	}
	@RequestMapping("/page/need/selectNoPassNeedByRouid.do")
	public String selectNoPassNeedByRouid(HttpServletRequest request,HttpServletResponse response){
		String rouid = request.getParameter("rouid");
		List<BsNeedInfor> list = this.userNeedService.selectNoPassNeedByRouid(rouid);
		request.setAttribute("list", list);
		return "main/report/needPass";
	}

		/**
		 * 删除需求信息
		 */
		@RequestMapping("/page/need/{needId}/docloseNeed.do")
		public String docloseNeed( HttpServletRequest request,HttpServletResponse response,@PathVariable String needId ) throws JTransException, JSystemException{
			JAppContext context = serviceFactory.buildAppContextWithUser(request,response);		//buildAppContextWithUser 自带客户信息.
	 		String needName = request.getParameter("needName");
	 		BsUserInfor user = context.getUserInfo();
	 		BsNeedInfor req = new BsNeedInfor();
	 		req.setProTeamNo(user.getProTeamNo());
	 		if(StringUtil.isNotEmpty(needName)){
		 		req.setNeedName(needName);
		 		req.setNeedNo(needName);
	 		}
			List<BsNeedInfor> needList = this.userNeedService.selectNeedInfor(req);
			request.setAttribute("needList", needList);
			return "page/base/dologin.do"; 
		}
}
